package com.medical.admin.cost.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.cost.common.dto.IndentDto;
import com.medical.admin.cost.common.dto.OutpatientInfo;
import com.medical.admin.patient.common.dto.PatientDto;
import com.medical.admin.cost.entity.Indent;
import com.medical.admin.patient.entity.Patient;
import com.medical.admin.cost.ex.OutpatientException;
import com.medical.admin.cost.mapper.IndentMapper;
import com.medical.admin.patient.mapper.PatientMapper;
import com.medical.admin.cost.service.OutpatientConsumptionService;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OutpatientConsumptionServiceImpl implements OutpatientConsumptionService {
    @Resource
    private IndentMapper indentMapper;
    @Resource
    private PatientMapper patientMapper;


    @Override
    public IPage<OutpatientInfo> searchOutpatientList(OutpatientInfo outpatientInfo, Integer page, Integer pageSize) {
        List<OutpatientInfo> infoList = indentMapper.selectPageList(outpatientInfo);
        if (ObjectUtils.isEmpty(infoList)){
            throw new OutpatientException(ResponseCode.OUTPATIENT_NULL_ERROR);
        }
        return listToPage(infoList,page,pageSize);

    }

    @Override
    public Integer addOutpatient(OutpatientInfo outpatientInfo) {
        Patient patient = BeanConvertUtils.convertTo(outpatientInfo.getPatientDto(), Patient::new);
        int count = patientMapper.insert(patient);
        if (count <= 0){
            throw new OutpatientException(ResponseCode.PATIENT_ADD_ERROR);
        }
        Indent indent = BeanConvertUtils.convertTo(outpatientInfo.getIndentDto(), Indent::new);
        indent.setPatientId(patient.getPatientId());
        count = indentMapper.insert(indent);
        if (count <= 0){
            throw new OutpatientException(ResponseCode.PATIENT_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeOutpatient(Long indentId) {
        int count = indentMapper.deleteById(indentId);
        if (count <= 0){
            throw new OutpatientException(ResponseCode.INDENT_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyOutpatient(OutpatientInfo outpatientInfo) {
        int count = patientMapper.updateByPatientId(BeanConvertUtils.convertTo(outpatientInfo.getPatientDto(), Patient::new));
        if (count <= 0){
            throw new OutpatientException(ResponseCode.PATIENT_UPDATE_ERROR);
        }
        count = indentMapper.updateByIndentId(BeanConvertUtils.convertTo(outpatientInfo.getIndentDto(), Indent::new));
        if (count <= 0){
            throw new OutpatientException(ResponseCode.INDENT_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public OutpatientInfo detailOutpatient(Long indentId) {
        Indent indent = indentMapper.selectById(indentId);
        if (indent == null){
            throw new OutpatientException(ResponseCode.INDENT_SELECT_ERROR);
        }
        Patient patient = patientMapper.selectById(indent.getPatientId());
        if (patient == null){
            throw new OutpatientException(ResponseCode.PATIENT_NULL_ERROR);
        }
        OutpatientInfo outpatientInfo = new OutpatientInfo();
        outpatientInfo.setPatientDto(BeanConvertUtils.convertTo(patient, PatientDto::new));
        outpatientInfo.setIndentDto(BeanConvertUtils.convertTo(indent, IndentDto::new));
        return outpatientInfo;
    }

    public static IPage<OutpatientInfo> listToPage(List list, int pageNum, int pageSize){
        List pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }
        IPage<OutpatientInfo> page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }




}
